import store from '@/store/index.js'
import Cache from "@/utils/cache.js"
import Routine from '@/libs/routine.js';
import {
	silenceAuth
} from '@/api/public';

const silenceAuthHandle = function(eqObj = null) {
	console.log(store.state.app.token, "获取token");
	if (store.state.app.token && !eqObj) {
		return new Promise(reslove => {
			reslove(store.state.app.token);
		})
	} else {
		return new Promise((reslove, reject) => {
			Routine.getCode().then(code => {
				let params = {
					code: code,
					...eqObj
				}
				silenceAuth(params).then(res => {
					const data = res.data
					let time = res.data.token.expire - Cache.time();
					store.commit('LOGIN', {
						token: res.data.token.token,
						time: time
					});
					if (data.phone) {
						Cache.set('phone', data.phone)
					}
					reslove(res)
				}).catch(err=>{
					reject(err)
					console.log(err, "静默授权接口报错");
				});
			}).catch(err => {
				reject(err)
				console.log(err, "获取code失败");
			});
		})
	}
}
export default silenceAuthHandle